package pojo.hibernate;

import utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.List;

public class StatemasterDAO {

    public void save(Statemaster state) {
        Session session = HibernateUtil.getSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.save(state);
            tx.commit();
        }
        catch (RuntimeException re) {
            if(state != null)
                tx.rollback();
            throw re;
        }
        finally {
            session.close();
        }
    }

    public List<Statemaster> findAll() {
        Session session = HibernateUtil.getSession();
        try {
            session.beginTransaction();
            List<Statemaster> list = session.createQuery("from Statemaster").list();
            return list;
        }
        finally {
            session.close();
            }
        }
    

    public List<Statemaster> findByCountryId(Byte countryId) {
        Session session = HibernateUtil.getSession();
        try {
            session.beginTransaction();
            List<Statemaster> smList = session.createQuery("Select u from Statemaster u where u.countrymaster.countryId = :countryId").setParameter("countryId", countryId).list();
            return smList;
        }
        finally {
            session.close();
            }
        }

    public List<Statemaster> findByCountryName(String countryName) {        
        Session session = HibernateUtil.getSession();
        try {
            session.beginTransaction();
            List<Statemaster> smList = session.createQuery("Select u from Statemaster u where u.countrymaster.countryName = :countryName").setParameter("countryName", countryName).list();
            return smList;
        }
        finally {
            session.close();
            }
        }

    }
